import 'dart:async';

import 'package:flutter/services.dart';

class StramChannelController {
  StramChannelController._internal();

  static StramChannelController _streamController =
      StramChannelController._internal();

  factory StramChannelController() {
    return _streamController;
  }

  static const EventChannel _eventChannel = EventChannel('main_stream_channel');

  StreamSubscription streamSubscription;
  //receive
  void init() {
    streamSubscription = _eventChannel
        .receiveBroadcastStream()//可以携带参数
        .listen(_onData, onError: _onError, onDone: _onDone);
  }

  void _onDone() {
    print("endofstream");
  }

  _onError(error) {
    PlatformException platformException = error;
    print(platformException.message);
  }

  void _onData(message) {
    print(message);
  }
}
